package com.example.reggie_take_out.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie_take_out.commen.R;
import com.example.reggie_take_out.entry.Employee;
import com.example.reggie_take_out.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;


    /**
     * 创建员工登录方法
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){


        //将得到的用户密码进行加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据前端提供的username查询用户
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername()); //相当于一种条件
        Employee one = employeeService.getOne(queryWrapper); //把条件装入语句
        //1.判断用户名是否存在
        if(one == null)
        {
            return R.error("用户名或密码错误！");
        }
        //2.判断密码是否错误
        if(!one.getPassword().equals(password))
        {
            return R.error("用户名或密码错误！");
        }
        //3.判断用户状态
        if(one.getStatus() == 0)
        {
            return R.error("用户已被冻结");
        }
        //4.登录成功 将用户id加入session
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }
    /**
     * 创建员工登出方法
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清除会话中保存的数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    /**
     * 创建添加员工的方法
     */
    @PostMapping
    public R<String> add(HttpServletRequest request,@RequestBody Employee employee){
        //所有用户创建时密码默认为123456 后面再由用户自己进行修改(使用md5进行加密)
        String s = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(s);
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //从会话中获得当前创建者的ID 并传入其中
        //employee.setCreateUser((Long)request.getSession().getAttribute("employee"));
        //employee.setUpdateUser((Long)request.getSession().getAttribute("employee"));

        //将用户传入数据库中
         employeeService.save(employee);

         return R.success("添加成功");


    }

    /**
     * 完成分页查询功能 用mybatis-plus的插件 pagination 和 page类
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize ,String name){
        //使用baomidou创建好的Page类 和page方法
        Page pageInfo = new Page(page,pageSize);
        //除了当前页号 和 页的大小之外 还可能有条件name 因此要用到Wrapper来创建条件
        LambdaQueryWrapper<Employee> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //增加结果排序条件
        wrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,wrapper); //自动将 查询结果封装到pageInfo中

        return R.success(pageInfo);
    }

    /**
     * 根据id进行修改操作
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){

       //employee.setUpdateUser((long)request.getSession().getAttribute("employee"));
        //employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 编辑功能 先根据id查询用户进行数据回显 然后@click后使用update方法 更新数据
     * @param  id
     * @return
     */
   @GetMapping("/{id}")
    public  R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("未知错误");

   }
}
